import { App } from "../../../src/App";
import { Singleton } from "../../../src/core/Class";
import { RedPointEvent } from "../../../src/core/EventConst";
import PackSys from "./PackSys";
import RedPointSys from "./RedPointSys";

interface Goods{
    id:number,
    count:number,
};
interface MailInfo{
    id:number,     // 邮件ID
    open:boolean,  // 是否打开
    timeStamp:number,   // 邮件时间
    sender:string, // 发件人
    annex:Goods[],// 附件
    text:string,   // 邮件内容
}
class CMailSys extends Singleton<CMailSys> {
    private _mailList:MailInfo[] = [];
    protected onInit(){
    }

    protected onDestroy(){
        this._mailList = [];
        cc.log('destroy mail system end.');
    }

    public updateMail(){
        this._mailList = [];
        if (App.getCfg('test')){
            const time = (new Date()).getTime();
            this._mailList.push({id:100000,open:false,timeStamp:time,sender:'贝游斗地主',annex:[{id:500000,count:3},{id:400000,count:10}],text:'测试带附件的邮件展示'});
            this._mailList.push({id:100001,open:false,timeStamp:time+10000,sender:'贝游斗地主',annex:[],text:'测试邮件附件'});
            this._mailList.push({id:100002,open:false,timeStamp:time+20000,sender:'贝游斗地主',annex:[],text:'经常接触法律文件翻译，尤其是合同翻译的小伙伴对“附件”这个词应该很熟悉了。以往，小编总是下意识地把这个词和“appendix”联系起来，但后来发现它在英文中的表达远没有这么简单。今天，我们就来看一看“附件”的多种译法。'});
            this._mailList.push({id:100003,open:false,timeStamp:time+30000,sender:'贝游斗地主',annex:[],text:'经常接触法律文件翻译，尤其是合同翻译的小伙伴对“附件”这个词应该很熟悉了。以往，小编总是下意识地把这个词和“appendix”联系起来，但后来发现它在英文中的表达远没有这么简单。今天，我们就来看一看“附件”的多种译法。'});
            this._mailList.push({id:100004,open:false,timeStamp:time+40000,sender:'贝游斗地主',annex:[],text:'经常接触法律文件翻译，尤其是合同翻译的小伙伴对“附件”这个词应该很熟悉了。以往，小编总是下意识地把这个词和“appendix”联系起来，但后来发现它在英文中的表达远没有这么简单。今天，我们就来看一看“附件”的多种译法。'});
        } else {
            // 发送服务器消息获取邮箱信息
        }
        RedPointSys.notify(RedPointEvent.NOTIFY_NUMBER,true,this._mailList.length);
    }
    
    public get mailList():MailInfo[]{ return this._sort();}

    public openMail(id:number){
        for (let it of this._mailList){
            if (it.id === id){
                it.open = true;
                return ;
            }
        }
    }

    public removeAnnex(id:number):boolean{
        for (let it of this._mailList){
            if (it.id === id){
                if (it.annex && it.annex.length > 0){
                    PackSys.addItem(it.annex) && (it.annex = []);
                    return true ;
                }
            }
        }

        return false;
    }

    /**
     * 批量删除邮件
     * @param list  
     * @param cb 
     * @returns 
     */
    public remove(list:number[],cb?:(is:boolean, all:MailInfo[])=>void){
        const call = (is:boolean)=>{
            if (cb){
                this._sort();
                cb(is, this.mailList || []);
            }
        };

        if (!list || list.length < 1) {
            call(false);
            return ;
        }

        while(list.length){
            const id = list.shift();
            let idx = 0;
            for (let it of this._mailList){
                if (it && it.id === id){
                    this._mailList.splice(idx,1);
                    break ;
                }
                idx++;
            }
        }

        call(true);
    }

    private _sort(){
        if (!this._mailList) return [];
        return this._mailList.sort((a,b)=>{
            if (a.annex !== b.annex){
                return b.annex.length - a.annex.length;
            } else if (a.open !== b.open){
                let aN = a.open ? 1:0;
                let bN = b.open ? 1:0;
                return bN-aN;
            } else if (a.timeStamp !== b.timeStamp){
                return b.timeStamp - a.timeStamp;
            }else a.id - b.id;
        });
    }
}

const MailSys:CMailSys = CMailSys.getInstance<CMailSys>(CMailSys);
export default MailSys;